package org.dromara.third.dnnyun.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.dromara.components.http.HttpRequestUtil;
import org.dromara.components.sysConfig.constant.SysConfigConstants;
import org.dromara.third.dnnyun.domain.bo.DyConsumeOrderBo;
import org.dromara.third.dnnyun.domain.bo.DyStoreAchievementBo;
import org.dromara.third.dnnyun.domain.bo.DyStoreBo;
import org.dromara.third.dnnyun.domain.bo.DyUserListBo;
import org.dromara.third.dnnyun.domain.comm.CommUrlParam;
import org.dromara.third.dnnyun.domain.vo.*;
import org.dromara.third.dnnyun.service.IDnnyunService;
import org.dromara.components.sysConfig.service.ISysConfigService;
import org.dromara.third.dnnyun.util.DySignature;
import org.springframework.stereotype.Service;

/**
 * @author: wangdm
 * @date 2024/02/19 23:37
 * @Description:
 */
@Service
public class DnnyunServiceImpl implements IDnnyunService {

    private String contentTypeJson = "application/json";

    /** 查省份列表 */
    private String provinceListUrl = "https://oss4.dnnyun.cn/region/province.json";

    /** 根据省份ID查市区镇列表 */
    private String cityListUrl = "https://oss4.dnnyun.cn/region/{provinceId}.json";

    /** 查工号列表 */
    private String userListUrl = "/client/open/management/open-api/user-list";

    /** 查区域门店列表 */
    private String storeListUrl = "/client/open/organizational/open-api/store-list";
    /** 查消费订单列表 */
    private String consumeOrderListUrl = "/client/open/stock/open-api/consume-order-list";
    /** 查消费订单列表 */
    private String storeAchievementListUrl = "/client/open/report-analysis/open-api/store-achievement-list";

    @Resource
    private ISysConfigService sysConfigService;

    private CommUrlParam signature(){
        CommUrlParam commUrlParam = new CommUrlParam();
        long timestamp = DateUtil.currentSeconds();
        String nonce = RandomUtil.randomString(6).toLowerCase();
        String apiKey = sysConfigService.selectConfigByKey(SysConfigConstants.DNNYUN_APIKEY);
        String accountId = sysConfigService.selectConfigByKey(SysConfigConstants.DNNYUN_ACCOUNTID);

        commUrlParam.setAccountId(accountId);
        commUrlParam.setNonce(nonce);
        commUrlParam.setTimestamp(timestamp);

        DySignature.signature(commUrlParam,apiKey);
        return commUrlParam;
    }

    private String getUrl(String url){
        String domain = getDomain();
        CommUrlParam commUrlParam = signature();
        StringBuffer urlBuffer = new StringBuffer();
        urlBuffer.append(domain).append(url).append("?accountId=").append(commUrlParam.getAccountId()).append("&nonce=").append(commUrlParam.getNonce()).append("&timestamp=").append(commUrlParam.getTimestamp()).append("&signature=").append(commUrlParam.getSignature());
        return urlBuffer.toString();
    }
    private String getDomain(){
        String domain = sysConfigService.selectConfigByKey(SysConfigConstants.DNNYUN_DOMAIN);
        return domain;
    }

    @Override
    public DyProvinceListResp getProvinceList() {
        String url = provinceListUrl;
        String respStr = HttpRequestUtil.get(url, StringUtils.EMPTY);
        DyProvinceListResp dyProvinceListResp = JSONUtil.toBean(respStr, DyProvinceListResp.class);
        return dyProvinceListResp;
    }

    @Override
    public DyCityListResp getCityList(Long provinceId) {
        String url = cityListUrl.replace("{provinceId}",String.valueOf(provinceId));
        String respStr = HttpRequestUtil.get(url, StringUtils.EMPTY);
        DyCityListResp dyCityListResp = JSONUtil.toBean(respStr, DyCityListResp.class);
        return dyCityListResp;
    }

    @Override
    public DyUserListResp getDyUserList(DyUserListBo dyUserListBo) {
        String url = getUrl(userListUrl);
        String respStr = HttpRequestUtil.formPost(url,dyUserListBo);
        DyUserListResp dyUserListResp = JSONUtil.toBean(respStr, DyUserListResp.class);
        return dyUserListResp;
    }

    @Override
    public DyStoreListResp getDyStoreList(DyStoreBo dyStoreBo) {
        if(null == dyStoreBo.getOperatorUserId()){
            String operatorUserId = sysConfigService.selectConfigByKey(SysConfigConstants.DNNYUN_OPERATORUSERID);
            if(StringUtils.isNotBlank(operatorUserId)){
                dyStoreBo.setOperatorUserId(Integer.valueOf(operatorUserId));
            }
        }
        String url = getUrl(storeListUrl);
        String respStr = HttpRequestUtil.formPost(url,dyStoreBo);
        DyStoreListResp dyStoreListResp = JSONUtil.toBean(respStr, DyStoreListResp.class);
        return dyStoreListResp;
    }

    @Override
    public DyStoreAchievementResp getDyStoreAchievementList(DyStoreAchievementBo dyStoreAchievementBo) {
        String url = getUrl(storeAchievementListUrl);
        String respStr = HttpRequestUtil.formPost(url,dyStoreAchievementBo);
        DyStoreAchievementResp dyStoreAchievementResp = JSONUtil.toBean(respStr, DyStoreAchievementResp.class);
        return dyStoreAchievementResp;
    }

    @Override
    public DyConsumerOrderListResp getDyConsumeOrderList(DyConsumeOrderBo dyConsumeOrderBo) {
        if(null == dyConsumeOrderBo.getOperatorUserId()){
            String operatorUserId = sysConfigService.selectConfigByKey(SysConfigConstants.DNNYUN_OPERATORUSERID);
            if(StringUtils.isNotBlank(operatorUserId)){
                dyConsumeOrderBo.setOperatorUserId(Integer.valueOf(operatorUserId));
            }
        }
        String url = getUrl(consumeOrderListUrl);
        String respStr = HttpRequestUtil.formPost(url,dyConsumeOrderBo);
        DyConsumerOrderListResp dyConsumerOrderListResp = JSONUtil.toBean(respStr, DyConsumerOrderListResp.class);
        return dyConsumerOrderListResp;
    }
}
